/**
 * @author blancsv@usi.ch
 * @author castellan@usi.ch
 * @author daniele.gili@usi.ch
 */

package ch.usi.inf.pf2;

import java.util.ArrayList;
import java.util.Timer;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;

// only for when I'll use the parseInt:  import java.lang.Integer;

//except for the index parameter, all the numbers must be of class Integer!

public class PanicSimMain implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ArrayList partList = new ArrayList();
	private ArrayList<Input> inputList = new ArrayList<Input>();

	private static Timer delay = new Timer();

	private boolean running = true;

	public static void main(String[] args) {

	}

	public Timer getTime() {
		return delay;
	}

	public void startCompile(PanicSimDrawingArea area) {
		

		for (int i = 0; i < partList.size(); i++) {
			if (partList.get(i) != null
					&& partList.get(i).getClass() == MegaInput.class) {
				((MegaInput) partList.get(i)).startCompile(area);
			}
		}
	}

	// leave it here
	public void getExit(int index) {

		for (int i = partList.size() - 1; i >= 0; i--) {
			if (partList.get(i) != null
					&& partList.get(i).getClass() == MegaOutput.class) {
				((MegaOutput) partList.get(i)).getOutput(index);

			}
		}

	}
	
	public void stopThread(){
		for (int i = 0; i < partList.size(); i++) {
			if (partList.get(i) != null
					&& partList.get(i).getClass() == MegaInput.class) {
				((MegaInput) partList.get(i)).stopCompile();
			}
		}
	
		
	}

	public int getNumberParts() {
		try {
			return partList.size();
		} catch (Exception e) {
			return 0;
		}
	}

	public Object getGate(int i) {
		return partList.get(i);
	}

	public void addPart(Object part) {
		partList.add(part);
	}

	public ArrayList<Input> addExtraInput(Input input) {
		inputList.add(input);
		return inputList;
	}

	public void removePart(ArrayList<CommonGate> gate) {
		for (int i = 0; i < partList.size(); i++) {
			for (int o = 0; o < gate.size(); o++) {
				if (partList.get(i) == gate.get(o)) {
					if (partList.get(i).getClass() != MegaOutput.class) {

						for (int f = 0; f < ((Output) partList.get(i + 1))
								.getNumberWire(); f++) {

							((Output) partList.get(i + 1)).getWire(f).getPin().setInput(null);
							
							int n = ((Output) partList.get(i + 1)).getWire(f)
									.getIndex();
							((Output) partList.get(i + 1)).removeOutput(f);
							partList.set(n, null);

						}
						partList.set(i + 1, null);
					}
					partList.set(i, null);

				}
			}
		}
	}

	public ArrayList getpartList() {
		return partList;
	}

	public void setPartList(ArrayList partList) {
		this.partList = partList;
	}

	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

}